package models

import "time"

type BMUCollect struct {
	Model
	ValueType string `json:"valueType" gorm:"value_type"`
	PointId int `json:"pointId" gorm:"point_id"`
	Value float64 `json:"value"`
	CollTime time.Time `json:"collTime" gorm:"coll_time"`
}

type BMUStatistics struct {
	Max float64 `json:"max"`
	Min float64 `json:"min"`
	Avg float64 `json:"avg"`
	CollTime time.Time `json:"collTime" gorm:"coll_time"`
}

func GetBMUCollects(bmuId, duration int) []BMUCollect {
	var bmuCollects, bmuVoltages, bmuSocs, bmuCurrents, bmuTemperatures, bmuSmokes []BMUCollect
	db.Raw("select id,'Voltage' as value_type,point_id,voltage as value,coll_time from bms_bmu_battery where bmu_id=? and to_days(now())-to_days(coll_time)<=?", bmuId, duration).Scan(&bmuVoltages)
	bmuCollects = append(bmuCollects, bmuVoltages...)
	db.Raw("select id,'SOC' as value_type,point_id,soc as value,coll_time from bms_bmu_battery where bmu_id=? and to_days(now())-to_days(coll_time)<=?", bmuId, duration).Scan(&bmuSocs)
	bmuCollects = append(bmuCollects, bmuSocs...)
	db.Raw("select id,'Current' as value_type,point_id,value,coll_time from bms_bmu_current where bmu_id=? and to_days(now())-to_days(coll_time)<=?", bmuId, duration).Scan(&bmuCurrents)
	bmuCollects = append(bmuCollects, bmuCurrents...)
	db.Raw("select id,'Temperature' as value_type,point_id,value,coll_time from bms_bmu_temperature where bmu_id=? and to_days(now())-to_days(coll_time)<=?", bmuId, duration).Scan(&bmuTemperatures)
	bmuCollects = append(bmuCollects, bmuTemperatures...)
	db.Raw("select id,'Smoke' as value_type,point_id,value,coll_time from bms_bmu_smoke where bmu_id=? and to_days(now())-to_days(coll_time)<=?", bmuId, duration).Scan(&bmuSmokes)
	bmuCollects = append(bmuCollects, bmuSmokes...)
	return bmuCollects
}

func GetBMUOutline(bmuId int) map[string]interface{} {
	data := make(map[string]interface{})
	var voltageStatistics, socStatistics, currentStatistics, smokeStatistics, temperatureStatistics BMUStatistics
	db.Raw("select max(voltage) as max,min(voltage) as min,avg(voltage) as avg,coll_time from (select max(coll_time) as t from bms_bmu_battery where bmu_id=? group by coll_time) a left join bms_bmu_battery b on a.t = b.coll_time where bmu_id=?", bmuId, bmuId).First(&voltageStatistics)
	data["voltage"] = voltageStatistics
	db.Raw("select max(soc) as max,min(soc) as min,avg(soc) as avg,coll_time from (select max(coll_time) as t from bms_bmu_battery where bmu_id=? group by coll_time) a left join bms_bmu_battery b on a.t = b.coll_time where bmu_id=?", bmuId, bmuId).First(&socStatistics)
	data["soc"] = socStatistics
	db.Raw("select max(value) as max,min(value) as min,avg(value) as avg,coll_time from (select max(coll_time) as t from bms_bmu_current where bmu_id=? group by coll_time) a left join bms_bmu_current b on a.t = b.coll_time where bmu_id=?", bmuId, bmuId).First(&currentStatistics)
	data["current"] = currentStatistics
	db.Raw("select max(value) as max,min(value) as min,avg(value) as avg,coll_time from (select max(coll_time) as t from bms_bmu_smoke where bmu_id=? group by coll_time) a left join bms_bmu_smoke b on a.t = b.coll_time where bmu_id=?", bmuId, bmuId).First(&smokeStatistics)
	data["smoke"] = smokeStatistics
	db.Raw("select max(value) as max,min(value) as min,avg(value) as avg,coll_time from (select max(coll_time) as t from bms_bmu_temperature where bmu_id=? group by coll_time) a left join bms_bmu_temperature b on a.t = b.coll_time where bmu_id=?", bmuId, bmuId).First(&temperatureStatistics)
	data["temperature"] = temperatureStatistics
	return data
}